package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ActivityAlertsQueryDTO;
import com.jeff.tianti.cms.dto.ConstitutionQueryDTO;
import com.jeff.tianti.cms.dto.SearchKewWordDto;
import com.jeff.tianti.cms.service.ActivityAlertsService;
import com.jeff.tianti.cms.service.ArticleService;
import com.jeff.tianti.cms.service.AssociationDocumentService;
import com.jeff.tianti.cms.service.ConstitutionService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
@CrossOrigin(maxAge = 3600)
public class SearchController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ActivityAlertsService activityAlertsService;

    @Autowired
    private AssociationDocumentService associationDocumentService;

    @Autowired
    private ConstitutionService constitutionService;

    @RequestMapping(value = "/keyWord/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public AjaxResult getKeyWord(@RequestBody SearchKewWordDto searchKewWordDto) {
        PageModel<Map<String, Object>> cmsArticleTitle = articleService.getByKewWord(searchKewWordDto);
        List<Map<String, Object>> cmsArticleKewWordList = cmsArticleTitle.getList();
        ActivityAlertsQueryDTO activityAlertsQueryDTO = new ActivityAlertsQueryDTO();
        activityAlertsQueryDTO.setKeyWord(searchKewWordDto.getKewWord());
        List<Map<String, Object>> activityAlertKewWordList = activityAlertsService.queryActivityAlertsTitleList(activityAlertsQueryDTO);
        List<Map<String, Object>> associationDocumentKewWordList = associationDocumentService.listTitleByKewWordAndId(searchKewWordDto.getKewWord(), "");
        ConstitutionQueryDTO constitutionQueryDTO = new ConstitutionQueryDTO();
        constitutionQueryDTO.setKewWord(searchKewWordDto.getKewWord());
        List<Map<String, Object>> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(cmsArticleKewWordList)) {
            cmsArticleKewWordList.forEach(stringObjectMap -> {
                String idValue = stringObjectMap.get("id").toString();
                stringObjectMap.put("id", "ca_" + idValue);
            });
            result.addAll(cmsArticleKewWordList);
//            CollectionUtils.mergeArrayIntoCollection(cmsArticleKewWordList, result);
        }
        if (!CollectionUtils.isEmpty(activityAlertKewWordList)) {
            activityAlertKewWordList.forEach(stringObjectMap -> {
                String idValue = stringObjectMap.get("id").toString();
                stringObjectMap.put("id", "aa_" + idValue);
            });
            result.addAll(activityAlertKewWordList);
//            CollectionUtils.mergeArrayIntoCollection(activityAlertKewWordList, result);
        }
        if (!CollectionUtils.isEmpty(associationDocumentKewWordList)) {
            associationDocumentKewWordList.forEach(stringObjectMap -> {
                String idValue = stringObjectMap.get("id").toString();
                stringObjectMap.put("id", "ad_" + idValue);
            });
            result.addAll(associationDocumentKewWordList);
//            CollectionUtils.mergeArrayIntoCollection(associationDocumentKewWordList, result);
        }

        return AjaxResult.setSuccessData(result);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public AjaxResult searchByKeyWord(String id) {
        String[] idStr = StringUtils.split(id, "_");
        String primaryKey = "";
        if (idStr.length == 2) {
            primaryKey = idStr[1];
        }
        if (id.contains("ca_")) {
            SearchKewWordDto searchKewWordDto = new SearchKewWordDto();
            searchKewWordDto.setId(primaryKey);
            return AjaxResult.setSuccessData(articleService.getByKewWord(searchKewWordDto).getList());
        }
        if (id.contains("aa_")) {
            ActivityAlertsQueryDTO activityAlertsQueryDTO = new ActivityAlertsQueryDTO();
            activityAlertsQueryDTO.setId(primaryKey);
            return AjaxResult.setSuccessData(activityAlertsService.queryActivityAlertsTitleList(activityAlertsQueryDTO));
        }

        if (id.contains("ad_")) {
            return AjaxResult.setSuccessData(associationDocumentService.listTitleByKewWordAndId("", primaryKey));
        }
        return AjaxResult.setSuccessData(null);
    }

}
